package com.qianniu.napi.admin.web;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.base.Strings;

import com.qianniu.napi.admin.entity.Xnbi;
import com.qianniu.napi.admin.entity.JsonDO;
import com.qianniu.napi.admin.entity.Jinfo;
import com.qianniu.napi.admin.entity.User;
import com.qianniu.napi.admin.service.IXnbiService;

import com.qianniu.napi.common.config.Constants;
import com.qianniu.napi.common.helper.HttpHelper;
import com.qianniu.napi.common.util.EncryptUtil;
import com.qianniu.napi.common.util.ObjectUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * <p>
 * 虚拟币表
 * </p>
 *
 * @author chenxuebin
 * @since 2019-02-24
 */
@Controller
public class XnbiController {

    private static final Logger logger = LoggerFactory.getLogger(XnbiController.class);

    @Autowired
    private IXnbiService xnbiService;

    @RequestMapping("admin/xnbi/index")
    public String index() {
        return "admin/xnbi";
    }

    @RequestMapping("/admin/xnbi/list")
    @ResponseBody
    public Page<Xnbi> list(@RequestParam(value = "offset", required = false) Integer offset,
                                @RequestParam(value = "status", required = false) Integer status,
                                @RequestParam(value = "search", required = false) String search,
                                @RequestParam(value = "limit", required = false) Integer limit) {
        Wrapper<Xnbi> wrapper = new EntityWrapper<Xnbi>();
        wrapper.eq("mid", ObjectUtil.getMid());
//        if (!Strings.isNullOrEmpty(search))
//            wrapper.like("order_no", search).or().like("remark", search);

        wrapper.orderBy("id", false);
        Page<Xnbi> page = xnbiService.selectPage(new Page<Xnbi>(offset/limit+1, limit), wrapper);
        return page;
    }

    @RequestMapping("/admin/xnbi/add")
    public String add(@Valid Xnbi bean, BindingResult bindingResult) {
        // 字段校验
        if( bindingResult.hasErrors())
        {
            logger.debug( "There are errors! {}", bindingResult );
            return null;
        }
        bean.setCreatetime(new Date());
        bean.setUid(ObjectUtil.getUid());
        bean.setMid(ObjectUtil.getMid());
        bean.setMpid(ObjectUtil.getMpid());
        bean.insert();
        return "admin/xnbi";
    }

    @RequestMapping("/admin/xnbi/getXnbiById")
    @ResponseBody
    public Xnbi getXnbiById(long id) {
        Xnbi xnbi = xnbiService.selectById(id);
        return xnbi;
    }

    @RequestMapping("/admin/xnbi/deleteOne")
    @ResponseBody
    public int deleteOne(long id) {
        boolean isSucc = xnbiService.deleteById(id);
        return isSucc ? 1 : 0;
    }

    @RequestMapping("/admin/xnbi/changeState")
    @ResponseBody
    public String changeState(long id, int state) {
        Xnbi bean = new Xnbi();
        bean.setId(id);
        bean.setState(state);
        bean.setModifytime(new Date());
        bean.setModifier(ObjectUtil.getUid());
        xnbiService.updateById(bean);
        return "success";
    }

    @RequestMapping("/admin/xnbi/modify")
    public String update(Xnbi bean) {
        bean.setModifytime(new Date());
        bean.setModifier(ObjectUtil.getUid());
        boolean isSuccc = xnbiService.updateById(bean);
        if (isSuccc) {
            return "redirect:index";
        }
        return "error";
    }

    /// / ************* bellow is xnbi interface for user *********

    @RequestMapping("/api/xnbi/list")
    @ResponseBody
    public Jinfo list(@RequestParam(value="status",required = false)String status,
                       @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                       @RequestParam(value = "pageSize", required = false) Integer pageSize,
                                HttpServletRequest req) {
        Jinfo info = new Jinfo();
        if(pageSize==null)pageSize = 10;
        Page page=new Page(pageIndex,pageSize);
        page = xnbiService.selectPage(page, new EntityWrapper<Xnbi>()
                .eq("uid", ObjectUtil.getUid())
                .eq("state", 1));
        info.setInfo(page.getRecords());
        return info;
    }

    @RequestMapping("/api/xnbi/add")
    @ResponseBody
    public Jinfo xnbiAdd(@Valid Xnbi xnbi,
                          BindingResult bindingResult,HttpServletRequest req) {
        Jinfo info = new Jinfo();
        // 字段校验
        if(bindingResult.hasErrors())
        {
            logger.debug( "There are errors! {}", bindingResult );
            info.setMsg("There are errors! "+bindingResult);
            info.setCode(400);
            return info;
        }
        xnbi.setCreatetime(new Date());
        xnbi.setMid(ObjectUtil.getMid());
        xnbi.setMpid(ObjectUtil.getMpid());
        xnbi.setUid(ObjectUtil.getUid());
        xnbi.insert();
        return info;
    }

    @RequestMapping("/api/xnbi/modify")
    @ResponseBody
    public Jinfo xnbiUpdate(HttpServletRequest req,
                                    Xnbi xnbi) {
        Jinfo info = new Jinfo();
        xnbi.setModifytime(new Date());
        xnbi.updateById();
        return info;
    }

    @RequestMapping("/api/xnbi/detail")
    @ResponseBody
    public Jinfo getXnbiDetailById(@RequestParam("token") String token,
                                       @RequestParam("id") long id,HttpServletRequest req
    ) {
        Jinfo info = new Jinfo();
        Xnbi xnbi = xnbiService.selectById(id);
        info.setInfo(xnbi);
        return info;
    }
}
